import { Controller } from "../../mvc/core/Controller";
import { IPetWareHouseController } from "./interfaces/IPetWareHouseController";
import { IPetWareHouseModule } from "../../../modules/interfaces/IPetWareHouseModule";
import { PetWareHouseModule } from "../../../modules/PetWareHouseModule";
import { PetMeetSelectPetWindow } from "../view/petMeet/PetMeetSelectPetWindow";
import { PetViewWindow } from "../view/pet/PetViewWindow";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { PetMeetEventName } from "../view/petMeet/info/PetMeetEventName";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { ServerCommand } from "../mvc/ServerCommand";
import { DataEvent } from "../events/DataEvent";
import { Global } from "../../../com/gengine/global/Global";
import { NpcEffectRule } from "../rules/NpcEffectRule";
import { SPetInfo } from "../../../Message/Game/SPetInfo";
import { GameProxy } from "../mvc/GameProxy";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { EffectManager } from "../manager/EffectManager";
import { SPetUpdate } from "../../../Message/Game/SPetUpdate";
import { EPlayerItemPosType } from "../../../Message/Public/EPlayerItemPosType";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
type int = number;
//class PetWareHouseController
    
    export  class PetWareHouseController extends Controller implements IPetWareHouseController
    {
       

        public get petWareHouseModule(): IPetWareHouseModule
        {
            if (this._petWareHouseModule == null) 
            {
                this._petWareHouseModule = new PetWareHouseModule();
            }
            return this._petWareHouseModule;
        }

        public get petMeetSelectPetWindow(): PetMeetSelectPetWindow
        {
            if (this._petMeetSelectPetWindow == null) 
            {
                this._petMeetSelectPetWindow = new PetMeetSelectPetWindow();
            }
            return this._petMeetSelectPetWindow;
        }

        public get petViewWindow(): PetViewWindow
        {
            if (this._petViewWindow == null) 
            {
                this._petViewWindow = new PetViewWindow();
            }
            return this._petViewWindow;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.PetStorageOpen, this.onPetStorageOpenHandler);
            Dispatcher.addEventListener(PetMeetEventName.ConfirmSelectedPet, this.onConfirmSelectedPetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_LookUpPet, this.onLookUpPetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_TakeOutPet, this.onTakeOutPetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_StorePet, this.onStorePetHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_ExpendCapacity, this.onExpendCapacityHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_ExpendCapacitySure, this.onExpendCapacitySureHandler);
            Dispatcher.addEventListener(EventName.PetBagUpdate, this.onPetBagUpdateHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_CloseWindow, this.onWindowCloseHandler);
            Dispatcher.addEventListener(EventName.PetReleaseSuccess, this.onPetReleaseSuccessHandler);
            Dispatcher.addEventListener(EventName.PetWareHouse_OrderChange, this.onOrderChangeHandler);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateWarehousePetInfo, this.onRecivePetInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetWareHouseCapacityChange, this.onBagCapacityChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetWareHousePetChange, this.onWareHousePetChangeHandler);
            return;
        }

        /* internal  */onPetStorageOpenHandler(arg1: DataEvent): void
        {
            this.setPetOrder();
            var loc1=(Global.stage.stageWidth - this.petWareHouseModule.width) / 2 - 200;
            var loc2=(Global.stage.stageHeight - this.petWareHouseModule.height) / 2;
            this.petWareHouseModule.show(loc1, loc2);
            this.petMeetSelectPetWindowOpen();
            NpcEffectRule.registDialogEffectWindow(this.petWareHouseModule);
            return;
        }

        /* internal  */setPetOrder(): void
        {
            var loc1=0;
            if (this._page != 1) 
            {
                if (this._page == 2) 
                {
                    loc1 = 8;
                }
            }
            else 
            {
                loc1 = 0;
            }
            var loc2=cache.petWareHouse.capacity;
            var loc3=cache.petWareHouse.petBag.dictIntStr;
            var loc4=loc1;
            while (loc4 < loc2) 
            {
                if (loc3[loc4 + 1] == null) 
                {
                    this._petOrder = loc4 + 1;
                    break;
                }
                ++loc4;
            }
            return;
        }

        /* internal  */petMeetSelectPetWindowOpen(): void
        {
            var loc1=0;
            var loc2=0;
            loc1 = this.petWareHouseModule.x + this.petWareHouseModule.width;
            loc2 = this.petWareHouseModule.y + this.petWareHouseModule.height - this.petMeetSelectPetWindow.height;
            this.petMeetSelectPetWindow.updateDataProvider(cache.pet.petInfos);
            this.petMeetSelectPetWindow.show(loc1, loc2);
            NpcEffectRule.registDialogEffectWindow(this.petMeetSelectPetWindow);
            return;
        }

        /* internal  */onConfirmSelectedPetHandler(arg1: DataEvent): void
        {
            var loc1=null;
            if (!(this._petMeetSelectPetWindow == null) && !this._petMeetSelectPetWindow.isHide) 
            {
                if (!cache.petWareHouse.isPetBagFull() > 0) 
                {
                    this._isStore = true;
                }
                loc1 = arg1.data as SPetInfo;
                if (loc1 != null) 
                {
                    GameProxy.petProxy.storeWarehousePet(this._petOrder, loc1.uid);
                }
            }
            return;
        }

        /* internal  */onLookUpPetHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            GameProxy.petProxy.lookOverWarehousePet(loc1);
            return;
        }

        /* internal  */onTakeOutPetHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            if (cache.pet.isPetBagFull()) 
            {
                MsgManager.showRollTipsMsg(Language.getString(35100));
            }
            else 
            {
                loc1 = arg1.data as Object;
                loc2 = loc1.petUid as string;
                this._petOrder = loc1.order as int;
                this._isTakeOut = true;
                GameProxy.petProxy.takeOutWarehousePet(loc2);
            }
            return;
        }

        /* internal  */onStorePetHandler(arg1: DataEvent): void
        {
            var loc1=null;
            var loc2=null;
            this._petOrder = arg1.data as int;
            if (this.petMeetSelectPetWindow.isHide) 
            {
                this.petMeetSelectPetWindowOpen();
            }
            else 
            {
                if (this._timer == null) 
                {
                    this._timer = new Object /* flash.utils.Timer */(200, 2);
                }
                this._timer.reset();
                this._timer.start();
                this._timer.addEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
                loc1 = new Object /* flash.filters.GlowFilter */(16777062, 1, 3, 2, 3);
                loc2 = [loc1];
                EffectManager.glowFilterReg(this.petMeetSelectPetWindow, loc2, 0.6, 10, 5, 0);
            }
            return;
        }

        /* internal  */onTimerCompleteHandler(arg1: Object /* flash.events.TimerEvent */): void
        {
            this._timer.stop();
            this._timer.removeEventListener(flash.events.TimerEvent.TIMER_COMPLETE, this.onTimerCompleteHandler);
            this._timer = null;
            EffectManager.glowFilterUnReg(this.petMeetSelectPetWindow);
            return;
        }

        /* internal  */onExpendCapacityHandler(arg1: DataEvent): void
        {
            this.petWareHouseModule.expendWindow.show();
            return;
        }

        /* internal  */onExpendCapacitySureHandler(arg1: DataEvent): void
        {
            this._expandType = arg1.data as int;
            GameProxy.petProxy.expandWarehouse(this._expandType);
            return;
        }

        /* internal  */onPetBagUpdateHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPetUpdate;
            if (this._isStore) 
            {
                this._isStore = false;
                this.petWareHouseModule.storePet(loc1.petInfo, this._petOrder);
            }
            else if (this._isTakeOut) 
            {
                this._isTakeOut = false;
                this.petWareHouseModule.takeOutPet(loc1.petInfo, this._petOrder);
            }
            if (this._petMeetSelectPetWindow != null) 
            {
                this._petMeetSelectPetWindow.updateDataProvider(cache.pet.petInfos);
            }
            return;
        }

        /* internal  */onWindowCloseHandler(arg1: DataEvent): void
        {
            if (!this.petMeetSelectPetWindow.isHide) 
            {
                this.petMeetSelectPetWindow.hide();
            }
            return;
        }

        /* internal  */onPetReleaseSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data.posType;
            var loc2=arg1.data.uid;
            if (!(this._petWareHouseModule == null) && !this._petWareHouseModule.isHide && loc1 == EPlayerItemPosType._EPlayerItemPosTypePetWarehouse) 
            {
                this._petWareHouseModule.deletePetSuccess(loc2);
            }
            return;
        }

        /* internal  */onOrderChangeHandler(arg1: DataEvent): void
        {
            this._petOrder = arg1.data as int;
            return;
        }

        /* internal  */onRecivePetInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SPetInfo;
            this.petViewWindow.updatePetInfoPanel(loc1);
            var loc2=this.petWareHouseModule.x + this.petWareHouseModule.width;
            var loc3=(Global.stage.stageHeight - this.petViewWindow.height) / 2;
            this.petViewWindow.show(loc2, loc3);
            NpcEffectRule.registDialogEffectWindow(this.petViewWindow);
            return;
        }

        /* internal  */onBagCapacityChangeHandler(arg1: Object): void
        {
            if (!(this._petWareHouseModule == null) && !this._petWareHouseModule.isHide) 
            {
                this._petWareHouseModule.expendCapacity(this._expandType);
            }
            if (!this._isStore) 
            {
                this.setPetOrder();
            }
            return;
        }

        /* internal  */onWareHousePetChangeHandler(arg1: Object): void
        {
            if (arg1 is int && arg1 as int > 0) 
            {
                this._page = arg1 as int;
            }
            if (!this._isStore) 
            {
                this.setPetOrder();
            }
            return;
        }

        private /* var */_petWareHouseModule: IPetWareHouseModule;

        private /* var */_petMeetSelectPetWindow: PetMeetSelectPetWindow;

        private /* var */_petViewWindow: PetViewWindow;

        private /* var */_petOrder: int=1;

        private /* var */_isStore: Boolean=false;

        private /* var */_isTakeOut: Boolean=false;

        private /* var */_timer: Object /* flash.utils.Timer */;

        private /* var */_page: int=1;

        private /* var */_expandType: int;
    }
